﻿
var syhUpload = (function () {

    function Singleton(args) {

        //设置args变量为接收的参数或者为空（如果没有提供的话）
        var args = args || {};
        return {
            upload_url: args.uploadUrl,
            button_placeholder_id: args.placeHoldId,
            flash_url: "/JS/swfUploadImg/Flash/swfupload.swf",
            file_size_limit: args.sizeLimit || "5 MB",
            file_types: args.fileType || "*.jpeg;*.jpg;*.png;*.gif",
            file_types_description: "All Files",
            file_upload_limit: args.file_upload_limit || 100,
            file_queue_limit: args.file_queue_limit || 10,
            post_params: args.post_params || {},
            //button_image_url: "/Script/swfUpload/XPButtonUploadText_61x22.png",
            button_text: '<span class="swfButton">上传多图</span>',
            button_text_style: ".swfButton { color: #ffffff; }",
            button_cursor: SWFUpload.CURSOR.HAND,
            button_text_left_padding: 10,
            button_text_top_padding: 2,
            button_width: args.width || "75",
            button_height: args.height || "21",
            queueContainId: args.queueContainId || "",
            removeClass: args.removeClass || "",
            moving_average_history_size: 40,
            button_window_mode: SWFUpload.WINDOW_MODE.TRANSPARENT,

            file_queued_handler: args.fileQueued || $.syhupload.fileQueued,
            file_queue_error_handler: args.fileQueueError || $.syhupload.fileQueueError,
            file_dialog_complete_handler: args.fileDialogComplete || $.syhupload.fileDialogComplete,
            upload_start_handler: args.uploadStart || $.syhupload.uploadStart,
            upload_progress_handler: args.uploadProgress || $.syhupload.uploadProgress,
            upload_success_handler: args.uploadSuccess || $.syhupload.uploadSuccess,
            upload_complete_handler: function (file) {
                if (this.getStats().files_queued === 0) {

                } else {
                    this.startUpload();
                }
            }
        };


    };




    //实例容器
    var swfu;
    var _static = {

        //获取实例的方法
        //返回Singleton的实例
        getInstance: function (args, reload) {

            if (swfu === undefined) {
                swfu = new SWFUpload(new Singleton(args));
            }
            else if (reload) {
                swfu = null;
                swfu = new SWFUpload(new Singleton(args));
            }
            return swfu;
        },
        removeFile: function (id) {
            $("#" + id).remove();
            swfu.cancelUpload(id, false);

        },
        removeFile: function (id, nowImgSize) {
            $("#" + id).remove();
            swfu.cancelUpload(id, false);
            imgCount -= 1;
            imgSizeCount -= nowImgSize;
            $("#spimgcount").html(imgCount);
            $("#spimgsizecount").html(SWFUpload.speed.formatBytes(imgSizeCount));
        },
        startUpload: function (file) {

            if (swfu.getStats().files_queued > 0) {
                swfu.startUpload(file);
            }
        }
    };
    return _static;
})();

//上传时间的回调函数对象
$.syhupload = {
    fileQueued: function (file) {
        try {
            alert("请完善相关fileQueued参数;");

        } catch (ex) {
            this.debug(ex);
        }
    },

    fileDialogComplete: function () {


    },
    fileQueueError: function (file, errorCode, message) {
        try {

            var errorName = "";
            //            if (errorCode === SWFUpload.errorCode_QUEUE_LIMIT_EXCEEDED) {
            //                errorName = "You have attempted to queue too many files.";
            //            }

            if (errorName !== "") {
                alert(errorName);
                return;
            }
            debugger;
            switch (errorCode) {
                case SWFUpload.QUEUE_ERROR.QUEUE_LIMIT_EXCEEDED:
                    alert("每次仅能上传10个文件!")
                    break;
                case SWFUpload.QUEUE_ERROR.FILE_EXCEEDS_SIZE_LIMIT:
                    alert("每个文件不能超过5 MB!");
                    break;
                case SWFUpload.QUEUE_ERROR.ZERO_BYTE_FILE:
                case SWFUpload.QUEUE_ERROR.INVALID_FILETYPE:
                default:
                    alert("请选择正确的文件格式");
                    break;
            }



        } catch (ex) {
            this.debug(ex);
        }

    },
    uploadStart: function () {


    },

    uploadProgress: function (file, bytesLoaded, bytesTotal) {


    },

    uploadSuccess: function (file, serverData) {
        try {

        } catch (ex) {
            this.debug(ex);
        }
    }


};
